Linux Shell美化:zsh的安装与调校
zsh
是Linux下一种更加强大的Shell。在Linux默认的Shell是bash
,zsh
则要比bash
强大很多。
在Linux下,可以通过命令来查看系统中安装的Shell:
cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
你也可以通过命令来查看当前使用的Shell:
echo $SHELL
/bin/bash
准备工作
我们首先需要做的就是把zsh
安装好。这里一共要安装两个东西,一个是zsh
本身,另一个是zsh
的简化配置工具oh-my-zsh
。
安装zsh
执行安装命令:
sudo apt install zsh
设置zsh
为默认Shell:
chsh -s /bin/zsh
重启终端,即可看到已经切换到zsh的模式。
当然如果想要界面更好看,最简单的方法是安装oh my zsh
。
安装oh my zsh
前置要求:系统已安装
git
根据oh my zsh
文档,你可以选择自动安装或者手动安装。
自动安装
三个命令任选其一可以使用的:
Method | Command |
---|---|
curl | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" |
wget | sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" |
fetch | sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" |
手动安装
从您还不知道的项目中检查安装脚本是个好主意。您可以通过首先下载安装脚本,查看它使一切看起来正常,然后运行它来做到这一点:
wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
sh install.sh
国内镜像安装
GitHub连接失败可能导致无法clone oh-my-zsh
并安装,这时候可以采用国内镜像源:Gitee 极速下载/oh-my-zsh。
你可以使用这个改装过的install.sh来进行手动安装:install.sh
zsh
的调校
环境变量
zsh 的配置主要集中在用户当前目录的.zshrc
里,用编辑器打开.zshrc
,其中有这么一行字:
# User configuration
这一行下面就都是用户配置的内容了,可以在此处定义自己的环境变量和别名,当然,oh my zsh
在安装时已经自动读取当前的环境变量并进行了设置,你可以继续追加其他环境变量,如:
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim' # vi自动换成vim
alias javac="javac -J-Dfile.encoding=utf8"
alias grep="grep --color=auto"
alias -s html=mate # 在命令行直接输入后缀为 html 的文件名,会在 TextMate 中打开
alias -s rb=mate # 在命令行直接输入 ruby 文件,会在 TextMate 中打开
alias -s py=vi # 在命令行直接输入 python 文件,会用 vim 中打开,以下类似
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'
zsh
的厉害之处在于不仅可以设置通用别名,还能针对文件类型设置对应的打开程序,比如:
alias -s html=mate
,意思就是你在命令行输入hello.html
,zsh会为你自动打开TextMat
并读取hello.html
。alias -s gz='tar -xzvf'
,表示自动解压后缀为.gz
的压缩包。
总之很强大。
主题
在.zshrc
里找到ZSH_THEME,就可以设置主题了,默认主题是:
ZSH_THEME=”robbyrussell”
oh my zsh
提供了数十种主题,相关文件在~/.oh-my-zsh/themes
目录下,你可以在Themes · ohmyzsh/ohmyzsh Wiki (github.com)预览它们,可以随意选择,也可以编辑主题满足自己的各种需求。我比较喜欢rkj-repos
这个主题。
诸多插件
zsh
的强大不光是因为他可以高度定制化,还拥有大量的社区插件内容,下面介绍几个主要并显著提高效率的。
色彩高亮插件
色彩高亮需要插件zsh-syntax-highlighting
来实现。
zsh-syntax-highlighting
安装
软件包安装(官方推荐)
使用命令:
sudo apt install zsh-syntax-highlighting
直接克隆修改~/.zshrc
文件
官方(GitHub):
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
我拉的镜像(Gitee):
git clone https://gitee.com/XiaolongSI/zsh-syntax-highlighting.git
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
然后需要重新载入Shell:
source ./zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
使用oh-my-zsh
等框架的方式安装(个人采用)
进入$ZSH_CUSTOM/plugins
,使用命令:
官方(GitHub):
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
我拉的镜像(Gitee):
git clone https://gitee.com/XiaolongSI/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
启用插件:
script
plugins=(
# other plugins...
zsh-syntax-highlighting
)
重新加载~/.zshrc
文件:
source ~/.zshrc
命令提示插件
命令提示需要插件zsh-autosuggestions
来实现。
zsh-autosuggestions
安装
软件包安装
可以通过软件包的形式安装,详见:zsh-autosuggestions/INSTALL.md
使用oh-my-zsh
等框架的方式安装(个人采用)
进入$ZSH_CUSTOM/plugins
,使用命令:
官方(GitHub):
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
我拉的镜像(Gitee):
git clone https://gitee.com/XiaolongSI/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
在~/.zshrc
中添加插件:
script
plugins=(
# other plugins...
zsh-autosuggestions
)
重启zsh:
source ~/.zshrc